package com.uchedao.seller.ui.car.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.uchedao.seller.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 蓝牙设备列表适配器
 * Created by zhengxizhen on 15/9/21.
 */
public class BlueDeviceAdapter extends BaseAdapter {

    private List<BluetoothDevice> list;
    private LayoutInflater inflater;

    public BlueDeviceAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void update(List<BluetoothDevice> _list) {
        if (list != null) {
            list.clear();
        }
        if (_list != null) {
            list = _list;
        }
        notifyDataSetChanged();
    }

    public void addDevice(BluetoothDevice device) {
        if (list == null) {
            list = new ArrayList<BluetoothDevice>();
        }
        list.add(device);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public BluetoothDevice getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_blue_tooth, null);
            convertView.setTag(viewHolder);
            viewHolder.tv_tooth_name_content = (TextView) convertView.findViewById(R.id.tv_tooth_name_content);
            viewHolder.tv_tooth_address_content = (TextView) convertView.findViewById(R.id.tv_tooth_address_content);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_tooth_address_content.setText(list.get(position).getAddress());
        viewHolder.tv_tooth_name_content.setText(list.get(position).getName());
        return convertView;
    }

    class ViewHolder {
        TextView tv_tooth_name_content;
        TextView tv_tooth_address_content;
    }
}
